package com.topofstack.flp.ui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.bappi.custom.activities.CustomActivity;
import com.topofstack.flp.FLPApplication;
import com.topofstack.flp.R;
import com.topofstack.flp.utils.Utils;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class VideosActivity extends CustomActivity implements OnClickListener {

	private static final int GALLERY_VIDEO_REQUEST_CODE = 203001;
	private static final int CAMERA_VIDEO_REQUEST_CODE = 203002;

	private Uri viUri = null;

	Button btnRecordAVideo;
	Button btnFromVideoLibrary;
	Button btnMyVideos;
	Button btnDelete;

	FLPApplication application;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_videos);

		application = ((FLPApplication) getApplication());

		btnRecordAVideo = (Button) findViewById(R.id.btnRecordAVideo);
		btnRecordAVideo.setOnClickListener(this);

		btnFromVideoLibrary = (Button) findViewById(R.id.btnFromVideoLibrary);
		btnFromVideoLibrary.setOnClickListener(this);

		btnMyVideos = (Button) findViewById(R.id.btnMyVideos);
		btnMyVideos.setOnClickListener(this);

		btnDelete = (Button) findViewById(R.id.btnDelete);
		btnDelete.setOnClickListener(this);

	}

	@Override
	public void onClick(View sender) {

		if (sender == btnRecordAVideo) {
			// ContentValues values = new ContentValues();
			// values.put(MediaStore.Video.Media.TITLE, "recorded");
			// values.put(MediaStore.Video.Media.DESCRIPTION,
			// "Video recorded by camera");
			// viUri = getContentResolver().insert(
			// MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
			// Intent cameraIntent = new
			// Intent(MediaStore.ACTION_VIDEO_CAPTURE);
			// cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, viUri);
			// startActivityForResult(cameraIntent, CAMERA_VIDEO_REQUEST_CODE);
			Intent cameraIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
			startActivityForResult(cameraIntent, CAMERA_VIDEO_REQUEST_CODE);
		} else if (sender == btnFromVideoLibrary) {
			Intent getVideoFromGalleryIntent = new Intent(Intent.ACTION_PICK);
			getVideoFromGalleryIntent.setType("video/*");
			startActivityForResult(getVideoFromGalleryIntent,
					GALLERY_VIDEO_REQUEST_CODE);
		} else if (sender == btnMyVideos) {
			Intent videoGalleryIntent = new Intent(this,
					VideoGalleryActivity.class);
			videoGalleryIntent.putExtra("is_delete", false);
			videoGalleryIntent.putExtra("is_export", false);
			videoGalleryIntent.putExtra("is_video", true);
//			startActivity(videoGalleryIntent);
			pushIntnet(videoGalleryIntent);
		} else if (sender == btnDelete) {
			Intent videoGalleryIntent = new Intent(this,
					VideoGalleryActivity.class);
			videoGalleryIntent.putExtra("is_delete", true);
			videoGalleryIntent.putExtra("is_export", false);
			videoGalleryIntent.putExtra("is_video", true);
//			startActivity(videoGalleryIntent);
			pushIntnet(videoGalleryIntent);
		}

	}

	@Override
	protected final void onActivityResult(final int requestCode,
			final int resultCode, final Intent i) {
		super.onActivityResult(requestCode, resultCode, i);

		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case GALLERY_VIDEO_REQUEST_CODE:
				Uri videoUri = i.getData();
				try {
					File file = Utils.convertVideoUriToFile(videoUri, this);
					String path = file.getAbsolutePath();
					SimpleDateFormat sdf = new SimpleDateFormat(
							"d MMM yyyy, hh_mm_ss aaa");
					String videoFileName = String.format("%s.3gp",
							sdf.format(new Date()));
					String videoFilePath = application.getAssetManager()
							.videoPathFromName(videoFileName);
					application.getAssetManager().moveFile(path, videoFilePath,this,true);
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;

			case CAMERA_VIDEO_REQUEST_CODE:
				
				try {
//					AssetFileDescriptor videoAsset = getContentResolver()
//							.openAssetFileDescriptor(i.getData(), "r");
//					SimpleDateFormat sdf = new SimpleDateFormat(
//							"d MMM yyyy, hh_mm_ss aaa");
//					String videoFileName = String.format("%s.3gp",
//							sdf.format(new Date()));
//					String videoFilePath = application.getAssetManager()
//							.videoPathFromName(videoFileName);
//					File dst = new File(videoFilePath);
//					FileInputStream fis = videoAsset.createInputStream();
//					FileOutputStream fos = new FileOutputStream(dst);
//
//					byte[] buffer = new byte[1024];
//					int length;
//					while ((length = fis.read(buffer)) > 0) {
//						fos.write(buffer, 0, length);
//					}
//					fis.close();
//					fos.close();
//					Log.i("DREG", "LEN="+dst.getAbsolutePath()+">"+dst.length());
					
					
					 Uri videoUrix = i.getData();
					 File file = Utils.convertVideoUriToFile(videoUrix,this);
					 String path = file.getAbsolutePath();
					 SimpleDateFormat sdf = new
					 SimpleDateFormat("d MMM yyyy, hh_mm_ss aaa");
					 String videoFileName = String.format("%s.3gp", sdf.format(new Date()));
					 String videoFilePath = application.getAssetManager().videoPathFromName(videoFileName);
					 application.getAssetManager().moveFile(path, videoFilePath,this,true);
					 
					 Log.i("DREG", "FILE="+path+" > : "+new File(videoFilePath).length());
					
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			}
		}

	}

}
